package com.depository.oss.interceptor;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.depository.common.utils.date.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.depository.common.utils.ipmac.IpHelper;

/** 
 * 白名单访问过滤
 */  
/**
 * 登陆拦截�?.
 *
 * @author guobo 2015/12/30 16:08
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {
	
	Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);
	/**
	 * 
	 * 
	 * 存管测试服务器IP
	 * 124.250.37.126 - 存管测试服务器
	 * 
	 * 
	 *  -存管正式服务器IP
	 * 58.83.140.37 - 支付平台测试服务器
	 * 
	 * 
	 * -北京资管平台正式服务器IP
	 * 47.94.103.245
	 * 
	 * 
	 * -北京借款平台正式服务器IP
	 * 101.200.61.127
	 * 
	 * 
	 * -沈阳投资平台正式服务器IP
	 *  10.0.6.119
	 *  10.0.6.120
	 * -北京小壹在线平台服务器IP
	 *  www.test.yiyicf.com-测试
	 *  
	 *  
	 *  -壹壹金服平台正式服务器IP
	 *  beta.yiyijinfu.com
	 *  betacore.yiyijinfu.com
	 */
	private static final String[] IGNORE_URI = {"/login"};
	
	private static final String[] YIYIINVERSTMENT_IP_ADDRESS = {"10.0.6.119","www.test.yiyicf.com","h5.test.yiyicf.com","betacore.yiyijinfu.com","10.0.6.120","beta.yiyijinfu.com","59.110.141.215","47.95.10.193","betam.yiyijinfu.com"};
	private static final String[] YIYIDATASYSTEM_IP_ADDRESS = {"47.94.103.245","47.95.8.110"};
	private static final String[] YIYIDATALOAN_IP_ADDRESS = {"101.200.61.127"};
	private static final String[] HAIKOU_IP_ADDRESS = {"124.250.37.126"};
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        boolean flag = false;
        String ip = IpHelper.getClientIpAddress(request);
        String ip_refere=null;
        String url_referer = request.getHeader("REFERER");
        String url = request.getRequestURL().toString();
        logger.info(DateUtils.getNowTime()+"====ip地址:"+ip+"====请求地址:"+url+"====url_referer====请求地址:"+url_referer);
        if(url_referer!=null && !"".equals(url_referer)){
        	int a=url_referer.indexOf("/")+2;
        	int b=0;
            b =url_referer.indexOf("/", 7);
            if(a>b){
            	b =url_referer.indexOf("/", 8);
            }
           url_referer=url_referer.substring(a, b); 	
           int c = url_referer.indexOf(":");
           if(c>0)
            	ip_refere=url_referer.substring(0, c);
            else
            	ip_refere=url_referer;
            for (String s : YIYIINVERSTMENT_IP_ADDRESS) {
                if (ip_refere.contains(s)) {
                    flag = true;
                    break;
                }
            }
            for (String s : YIYIDATASYSTEM_IP_ADDRESS) {
                if (ip_refere.contains(s)) {
                    flag = true;
                    break;
                }
            }
            for (String s : YIYIDATALOAN_IP_ADDRESS) {
                if (ip_refere.contains(s)) {
                    flag = true;
                    break;
                }
            }
        }
        for (String s : YIYIINVERSTMENT_IP_ADDRESS) {
            if (ip.contains(s)) {
                flag = true;
                break;
            }
        }
        for (String s : YIYIDATASYSTEM_IP_ADDRESS) {
            if (ip.contains(s)) {
                flag = true;
                break;
            }
        }
        for (String s : YIYIDATALOAN_IP_ADDRESS) {
            if (ip.contains(s)) {
                flag = true;
                break;
            }
        }
        for (String s : HAIKOU_IP_ADDRESS) {
            if (ip.contains(s)) {
                flag = true;
                break;
            }
        }
        for (String s : IGNORE_URI) {
            if (url.contains(s)) {
                flag = true;
                break;
            }
        }
        /*if (!flag) {
            response.sendRedirect(request.getContextPath() + "/login");
        }*/
        logger.info(DateUtils.getNowTime()+"====ip_refere="+ip_refere+"====flay="+flag);
        flag=true;
        return flag;
    }
 
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }
}